from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.ext import blobstore

import google.appengine.ext.blobstore
import datastore
import re

#class Reader():

#     query = 'select __key__ from __BlobInfo__'
#     print query
#     keys =  db.GqlQuery( query )
#     for k in keys:
#       t = db.get(k)
#       print t

class CopyPage(webapp.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write("reader <br>")

        facilityid=72
        query = 'select __key__ from _Membership where facility=' + str(facilityid)
        keys =  db.GqlQuery( query )
        for k in keys:
         t = db.get(k)
         t.delete()

        query = 'select __key__ from Membership where facility=' + str(facilityid)
        keys =  db.GqlQuery( query )
        for k in keys:
             t = db.get(k)
             g = datastore._Membership( key_name= r[0]+"_"+r[1] )
             g.facility= t.facility
             g.first = t.first
             g.last = t.last
             g.year = t.year
             g.email = t.email
             g.url = t.url
             g.address = t.address
             g.city = t.city
             g.state = t.state
             g.zip = t.zip
             g.areacode = t.areacode
             g.phone = t.phone
             g.gender = t.gender
             g.rating = t.rating
             g.netnotes = t.netnotes
             g.oksocial =  t.oksocial
             g.interest = t.interest
             g.help = t.help
             print( g.first , g.last, g.gender+g.rating , g.year ,g.oksocial,g.interest,g.help)
             plist.append( g)

class CleanPage(webapp.RequestHandler):
    def get(self):
        facilityid=72
        facilityid=3483
        query = 'select __key__ from Membership where facility=' + str(facilityid) + " order by last"
        self.response.out.write(query+"<br>")

        keys =  db.GqlQuery( query )
        for k in keys:
         t = db.get(k)
         self.response.out.write(t.last + ", " + t.first + " " + "<br>")
#        t.delete()

        self.response.out.write("clean<br>")


class Reader(webapp.RequestHandler):

    def get(self,fac):
        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write("reader <br>")
        self.response.out.write(fac + "<br>")

        if( fac == "readpatc"):
          self.response.out.write(" calling readpatc<br>")
          self.readpatc()
        elif( fac == "readsctc"):   
          self.response.out.write(" calling readsctc<br>")
          self.readsctc()

    def readsctc(self):

        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write("reader <br>")

        facilityid="3483"

        query = 'select __key__ from Membership where facility=' + str(facilityid)
        keys =  db.GqlQuery( query )
        for k in keys:
         t = db.get(k)
         t.delete()


        query = "where filename='sctc.csv' order by creation desc" 

        blob = blobstore.BlobInfo.gql( query )

        print(query)
        print( blob.count() )
        if( blob.count()<1 ): return
       

        print("read sctc")
        b = blob[0]      
        blob_key = b.key()
        blob_reader = blobstore.BlobReader(blob_key)

        playerlist= []                           # List to hold members
        regphone = re.compile("([\d]*)-([\d]*)-([\d]*)",re.IGNORECASE )

        for line in blob_reader:
           if( re.match("Last",line)): continue


           u = line.rsplit(",")                  # Split the line delimited by ,
           r = [ n.strip(" \r\n") for n in u ]   # Strip out end whitepace (includes \r\n )

           if( len(r) < 3): continue
           last = r[0]
           first = r[1]
           address = r[2] 
           city = r[3]
           zip = r[4]
           hphone = r[5]
           wphone = r[6]
           rating = r[7]
           email = r[8]
           email = email.strip("@")

#          Membership Year
           year = "2011"
           if(re.search("#",last)): year  = "2012"

#          Last name ( strip out *#)
           last = last.strip("*#")

#          City (fix up Santa Clara errors )
           if(re.search("clara",city,re.IGNORECASE)): city = "Santa Clara"

#          Add .0 to rating if necessary
           rating = rating.strip(" ")
           if(len(rating) == 1): rating  = rating+".0"

#          Fix phone (unlikely)
#          mhome = regphone.findall( hphone )
#          mwork = regphone.findall( wphone )


           g = datastore.Membership( key_name= str(facilityid)+"_"+first+"_"+last )
           g.facility= int(facilityid)
           g.first = first
           g.last = last
           g.year = int(year)
           g.email = email
           g.url = ""
           g.city = city
           g.zip = zip
           g.rating = rating
           playerlist.append( g )

           print( last,first,year,email,city,zip,rating)

#       Put everyone in at the same time
        db.put( playerlist )

    def readpatc(self):
        nl="<br>"

        self.response.headers['Content-Type'] = 'text/html'
        self.response.out.write("reader <br>")

        p = blobstore.BlobInfo.kind()
        print(  p )
        p = blobstore.BlobInfo.properties()
        print(  p )

        blob = blobstore.BlobInfo.all()
        query = "where filename='patc.csv' order by creation desc" 
        print ( query )


        blob = blobstore.BlobInfo.gql( query )
        print (blob.count() )
        for s in blob:
          print s.creation,s.filename,s.content_type,s.size

        b = blob[0]      
        blob_key = b.key()
#       r=db.get(blob_key)
#       print( r.filename )

#       print( blob_key.filename )
        print( "opening for read ")

        blob_reader = blobstore.BlobReader(blob_key)

        facilityid=72
        query = 'select __key__ from Membership where facility=' + str(facilityid)
        print(query+nl)

        keys =  db.GqlQuery( query )
        for k in keys:
         t = db.get(k)
         t.delete()


        blob_reader.readline()                   # Skip the first line (header information)
        playerlist= []                           # List to hold members
        for line in blob_reader:

             u = line.rsplit(",")                  # Split the line delimited by ,
             r = [ n.strip(" \r\n") for n in u ]   # Strip out end whitepace (includes \r\n )

             g = datastore.Membership( key_name= r[0]+"_"+r[1] )
             g.facility= facilityid
             g.first = r[0]
             g.last = r[1]
             g.year = int(r[2])    #read as string, but stored at int
             g.email = r[3]
             g.url = r[4]
             g.address = r[5]
             g.city = r[6]
             g.state = r[7]
             g.zip = r[8]
             g.areacode = r[9]
             g.phone = r[10]
             g.gender = r[11]
             g.rating = r[12]
             g.netnotes = r[13]
             g.oksocial = r[14] 
             g.interest = list(r[15].split(" ") )
             g.help = list(r[16].strip(" \r\n").split(" "))

             print( g.first , g.last, g.gender+g.rating , g.year ,g.oksocial,g.interest,g.help )
             playerlist.append( g )

#       Put everyone in at the same time
        db.put( playerlist )

application = webapp.WSGIApplication(
                                     [('/(readpatc)', Reader),
                                     ('/(readsctc)', Reader),
                                     ('/copy', CopyPage),
                                     ('/clean', CleanPage)],
                                     debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()
